package com.school.manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysCodeBook;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysCodeBookPo;
import com.school.manager.entity.vo.SysCodeBookVo;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.StatusEnum;
import com.school.manager.service.ISysCodeBookService;
import com.school.manager.utils.ConvertUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：系统字典控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "系统字典")
@RequestMapping(Constants.PROGRAM_PATH + "codebook")
public class SysCodeBookController {

	@Autowired
    private ISysCodeBookService codeBookService;
	
	@PostMapping("add")
    @ApiOperation("新增系统字典")
    @ApiImplicitParam(name = "系统字典", value = "codeBookPo", dataTypeClass = SysCodeBookPo.class)
    public ResponseEntity add(@RequestBody SysCodeBookPo codeBookPo) {
        return ResponseEnum.SUCCESS.success(codeBookService.save(ConvertUtil.convertObj(codeBookPo, SysCodeBook.class)));
    }


    @PostMapping("edit")
    @ApiOperation("修改系统字典")
    @ApiImplicitParam(name = "系统字典", value = "codeBookPo", dataTypeClass = SysCodeBookPo.class)
    public ResponseEntity edit(@RequestBody SysCodeBookPo codeBookPo) {
        return ResponseEnum.SUCCESS.success(codeBookService.updateById(ConvertUtil.convertObj(codeBookPo, SysCodeBook.class)));
    }


    @GetMapping("get/{id}")
    @ApiOperation("系统字典详情")
    @ApiImplicitParam(name = "系统字典主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
    	SysCodeBook codeBook = codeBookService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(codeBook, SysCodeBookVo.class));
    }


    @PostMapping("page")
    @ApiOperation("系统字典分页列表")
    @ApiImplicitParam(name = "系统字典Po", value = "codeBookPo", dataTypeClass = SysCodeBookPo.class)
    public ResponseEntity page(@RequestBody SysCodeBookPo codeBookPo) {
        return ResponseEnum.SUCCESS.success(codeBookService.page(codeBookPo));
    }
    
    @GetMapping("listbycode")
    @ApiOperation("根据code获取系统字典列表")
    @ApiImplicitParam(name = "代码", value = "codeType", dataTypeClass = String.class)
    public ResponseEntity listByCode(@RequestParam("codeType") String codeType) {
    	List<SysCodeBook> list = codeBookService.list(new LambdaQueryWrapper<SysCodeBook>()
    			.eq(SysCodeBook::getCodeType, codeType)
    			.eq(SysCodeBook::getStatus, StatusEnum.ENABLE.getCode()));
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertList(list, SysCodeBookVo.class));
    }
    
    @GetMapping("del/{id}")
    @ApiOperation("删除系统字典")
    @ApiImplicitParam(name = "系统字典主键", value = "id", dataTypeClass = Integer.class)
    public ResponseEntity del(@PathVariable("id") String id) {
    	SysCodeBook codeBook = new SysCodeBook();
    	codeBook.setId(id);
    	codeBook.setStatus(StatusEnum.DISABLE.getCode());
        return ResponseEnum.SUCCESS.success(codeBookService.updateById(codeBook));
    }
    
}
